/*
 *  This file is part of ZiggyStardust.
 *
 *  ZiggyStardust is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  ZiggyStardust is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.wisc.csvParser.plugins;
/**
 *
 * @author glocke-ou
 */
public class JDialogDateTimeParserTableBasedChoice extends java.awt.Dialog {

    DateTimeParserTableBasedChoice parser;

    /** Creates new form JDialogDateParserCSIArray */
    public JDialogDateTimeParserTableBasedChoice(java.awt.Frame parent, 
            boolean modal,DateTimeParserTableBasedChoice parser) {
        super(parent, modal);
        initComponents();
        
        this.parser = parser;

        // Initialize with default values
        yearTF.setText(Integer.toString(parser.getYearIndex()));
        monthTF.setText(Integer.toString(parser.getMonthIndex()));
        daynumTF.setText(Integer.toString(parser.getDayIndex()));       
        
        if(parser.getHourIndex() > -1){
            hasHourCB.setSelected(true);
            hourTF.setEnabled(true);
            hourTF.setText(Integer.toString(parser.getHourIndex()));
        }else{
            hourTF.setEnabled(false);
            hourTF.setText("");
        }

        if(parser.getMinutesIndex() > -1){
            hasMinutesCB.setSelected(true);
            minutesTF.setEnabled(true);
            minutesTF.setText(Integer.toString(parser.getMinutesIndex()));
        }else{
            minutesTF.setEnabled(false);
            minutesTF.setText("");
        }

        if (parser.getSecondsIndex() > -1) {
            hasSecondsCB.setSelected(true);
            secondsTF.setEnabled(true);
            secondsTF.setText(Integer.toString(parser.getSecondsIndex()));
        } else {
            secondsTF.setEnabled(false);
            secondsTF.setText("");
        }

        // Has a jPanel mask, so need the following two lines to complete initialization
        this.add(jPanel1);
        this.setSize(390,295);
        this.setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the FormEditor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        asdfasdf = new javax.swing.JLabel();
        yearTF = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        daynumTF = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        hourTF = new javax.swing.JTextField();
        saveButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        hasHourCB = new javax.swing.JCheckBox();
        secondsTF = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        hasSecondsCB = new javax.swing.JCheckBox();
        jLabel5 = new javax.swing.JLabel();
        monthTF = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        hasMinutesCB = new javax.swing.JCheckBox();
        minutesTF = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        asdfasdf.setText("Year Column Number:");

        yearTF.setText("1");

        jLabel1.setText("Day Column Number:");

        daynumTF.setText("3");

        jLabel2.setText("Hour Column Number:");

        hourTF.setText("4");

        saveButton.setText("Save");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        hasHourCB.setText("Has Hour");
        hasHourCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hasHourCBActionPerformed(evt);
            }
        });
        hasHourCB.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                hasHourCBPropertyChange(evt);
            }
        });

        secondsTF.setText("6");

        jLabel4.setText("Seconds Column Number:");

        hasSecondsCB.setText("Has Seconds");
        hasSecondsCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hasSecondsCBActionPerformed(evt);
            }
        });
        hasSecondsCB.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                hasSecondsCBPropertyChange(evt);
            }
        });

        jLabel5.setText("Month Column Number:");

        monthTF.setText("2");

        jLabel3.setText("<html>IMPORTANT: All column indexes are zero indexed. This<br />\nmeans the first column is column 0, the second is column 1,<br />\netc.</html>\n");

        hasMinutesCB.setText("Has Minutes");
        hasMinutesCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hasMinutesCBActionPerformed(evt);
            }
        });
        hasMinutesCB.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                hasMinutesCBPropertyChange(evt);
            }
        });

        minutesTF.setText("5");

        jLabel6.setText("Minutes Column Number:");

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(36, 36, 36)
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(jLabel2)
                            .add(jLabel1)
                            .add(asdfasdf)
                            .add(jLabel5)
                            .add(cancelButton)
                            .add(jLabel6)
                            .add(jLabel4))
                        .add(18, 18, 18)
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jPanel1Layout.createSequentialGroup()
                                .add(minutesTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 61, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                .add(hasMinutesCB))
                            .add(jPanel1Layout.createSequentialGroup()
                                .add(secondsTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 61, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                .add(hasSecondsCB))
                            .add(saveButton)
                            .add(jPanel1Layout.createSequentialGroup()
                                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, yearTF)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, monthTF)
                                    .add(hourTF)
                                    .add(daynumTF, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                .add(hasHourCB))))
                    .add(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jLabel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(26, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(yearTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(asdfasdf))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(monthTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(daynumTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(hourTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(hasHourCB))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel6)
                    .add(minutesTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(hasMinutesCB))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(secondsTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(hasSecondsCB))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 8, Short.MAX_VALUE)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cancelButton)
                    .add(saveButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        int yearIndex = -1;
        int monthIndex = -1;
        int daynumIndex = -1;
        int hourIndex = -1;
        int minutesIndex = -1;
        int secondsIndex = -1;

        try{
            yearIndex = Integer.parseInt(yearTF.getText());
            monthIndex = Integer.parseInt(monthTF.getText());
            daynumIndex = Integer.parseInt(daynumTF.getText());
            if (hasSecondsCB.isSelected()) {
                secondsIndex = Integer.parseInt(secondsTF.getText());
            }
            if(hasHourCB.isSelected()){
                hourIndex = Integer.parseInt(hourTF.getText());
            }
            if(hasMinutesCB.isSelected()){
                minutesIndex = Integer.parseInt(minutesTF.getText());
            }

        }catch(NumberFormatException e){
            javax.swing.JOptionPane.showMessageDialog(null,"Indexes must be positive integers or zero.");
            return;
        }

        // A check for positive indexes
        if(yearIndex < 0 || monthIndex < 0 || daynumIndex < 0 ||
                (hourIndex < 0 && hasHourCB.isSelected()) ||
                (minutesIndex < 0 && hasMinutesCB.isSelected()) ||
                (secondsIndex < 0 && hasSecondsCB.isSelected())){
            javax.swing.JOptionPane.showMessageDialog(null,
                    "All indexes must be integers zero or greater.");
            return;
        }

        parser.setDayIndex(daynumIndex);
        parser.setHourIndex(hourIndex);
        parser.setMinutesIndex(minutesIndex);
        parser.setYearIndex(yearIndex);
        parser.setMonthIndex(monthIndex);
        parser.setSecondsIndex(secondsIndex);

        //if we've gotten here, user wants to exit and input is all ok.
        setVisible(false);
        dispose();

    }//GEN-LAST:event_saveButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        setVisible(false);
        dispose();
}//GEN-LAST:event_cancelButtonActionPerformed

    // Not sure which of the below two methods sets it, so included both
    private void hasHourCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hasHourCBActionPerformed
        hourTF.setEnabled(hasHourCB.isSelected());
}//GEN-LAST:event_hasHourCBActionPerformed

    private void hasHourCBPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_hasHourCBPropertyChange
        hourTF.setEnabled(hasHourCB.isSelected());
}//GEN-LAST:event_hasHourCBPropertyChange

    private void hasSecondsCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hasSecondsCBActionPerformed
        secondsTF.setEnabled(hasSecondsCB.isSelected());
}//GEN-LAST:event_hasSecondsCBActionPerformed

    private void hasSecondsCBPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_hasSecondsCBPropertyChange
        secondsTF.setEnabled(hasSecondsCB.isSelected());
}//GEN-LAST:event_hasSecondsCBPropertyChange

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed

    }//GEN-LAST:event_formWindowClosed

    private void hasMinutesCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hasMinutesCBActionPerformed
        minutesTF.setEnabled(hasMinutesCB.isSelected());
    }//GEN-LAST:event_hasMinutesCBActionPerformed

    private void hasMinutesCBPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_hasMinutesCBPropertyChange
        minutesTF.setEnabled(hasMinutesCB.isSelected());
    }//GEN-LAST:event_hasMinutesCBPropertyChange


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel asdfasdf;
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField daynumTF;
    private javax.swing.JCheckBox hasHourCB;
    private javax.swing.JCheckBox hasMinutesCB;
    private javax.swing.JCheckBox hasSecondsCB;
    private javax.swing.JTextField hourTF;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField minutesTF;
    private javax.swing.JTextField monthTF;
    private javax.swing.JButton saveButton;
    private javax.swing.JTextField secondsTF;
    private javax.swing.JTextField yearTF;
    // End of variables declaration//GEN-END:variables

}
